package multithreading.Demo7_Thread8Blocl;

public class Second
{

    /**
     * 使用类中的一个对象 调用两个方法，同一时刻只能有一个方法有锁，其他方法必须等待当前锁释放才能执行
     */
    public static void main(String[] args) {

        Number2 number = new Number2();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    number.getOne();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                number.getTwo();
            }
        }).start();
    }


}

class Number2{
    public synchronized void getOne() throws InterruptedException {
        Thread.sleep(3000);
        System.out.println("=====one=====");
    }
    public synchronized void getTwo(){
        System.out.println("=====two=====");
    }
}
